package com.example.hospital.receiver;

import com.example.common.rabbit.service.RabbitService;
import com.example.common.util.constant.CommonUtilConstants;
import com.example.common.util.utils.StringUtils;
import com.example.hospital.service.ScheduleService;
import com.example.model.domain.entity.hospital.Schedule;
import com.example.model.vo.order.OrderMqVo;
import com.example.model.vo.sms.SmsVo;
import com.rabbitmq.client.Channel;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author juzipi
 * @Date 2021/7/29 19:41
 * @Info
 */
@Component
public class HospitalReceiver {

    public final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RabbitService rabbitService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = CommonUtilConstants.ORDER_QUEUE),
            exchange = @Exchange(value = CommonUtilConstants.ORDER_EXCHANGE),
            key = {CommonUtilConstants.ORDER_ROUTING}
    ))
    public void orderConsumer(OrderMqVo orderMqVo, Channel channel, Message message) throws IOException {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            logger.info("订单信息已消费 ----------> "+orderMqVo.getAvailableNumber());
            if (Integer.parseInt(orderMqVo.getSmsVo().getTemplateCode()) == 2){
                //2 为下单成功,更新预约数
                Schedule schedule = scheduleService.getScheduleByScheduleId(orderMqVo.getScheduleId());
                schedule.setReservedNumber(orderMqVo.getReservedNumber());
                schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
                Integer integer = scheduleService.updateSchedule(schedule);
            }
            if (Integer.parseInt(orderMqVo.getSmsVo().getTemplateCode()) == 1){
                //1 为取消预约,更新预约数
                Schedule schedule = scheduleService.getScheduleByScheduleId(orderMqVo.getScheduleId());
                Integer availableNumber = schedule.getAvailableNumber() + 1;
                schedule.setAvailableNumber(availableNumber);
                scheduleService.updateSchedule(schedule);
            }
            //暂时发不了短信，没有模板
            SmsVo smsVo = orderMqVo.getSmsVo();
            //1是退款后预约更新的邮件
            rabbitService.sendEmail(CommonUtilConstants.SMS_EXCHANGE,CommonUtilConstants.SMS_ROUTING,smsVo);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("消息未成功消费,返回消息队列 -------------> "+e.getMessage());
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }

    }





}
